define(['jquery', 'knockout', 'text!./data_status.html', 'uui'],
	function($, ko, template) {
	var ctrlBasePath =$ctx;
	var app;
	
	//定义数据模型
	var viewModel={
		data:ko.observable({}),
		md: document.querySelector('#demo-mdlayout'),
		mainDataTable: new u.DataTable({
			meta:{
				'dw_code':{type:'string'},
				'dw_name':{type:'string'},
				'p_dw_code':{type:'string'},
				'acc_book':{type:'string'},
				'gl_def':{type:'string'},
				'asset_card':{type:'string'}
			}
		}),
		
		
	/*	testDataTable: new u.DataTable({
			meta:{
				'dw_code':'101',
				'dw_name':'电费',
				'p_dw_code':'10',
				'acc_book':'2',
				'gl_def':'3',
				'asset_card':'25'
			}
		}),*/
		
		ipCompDataTable:new u.DataTable({
			meta:{
				'co_id':{type:'string'},				
				'hirer_id':{type:'string'},
				'co_code':{type:'string'},
				'parent_co_id':{type:'string'},
				'co_name':{type:'string'},
				'co_fullname':{type:'string'},
				'linkman':{type:'string'},
				'is_enabled':{type:'string'},
				'co_desc':{type:'string'},
				'level_num':{type:'string'},
				'dept_detail':{type:'string'},
				'disp_order':{type:'string'},
				'agcfs_dw_type':{type:'string'}				
			}
		}),
	};
	
	    //点击查询按钮以后调用的function，获取列表信息
	    viewModel.queryRowListInfo=function(){
	    	var nd_value = $("#select_nd_info").val();
	    	var dw_value = $("#select_dw_info").val();
	    	var co_code="";
	    	var parent_co_id="";	    	
	    	var info=viewModel.ipCompDataTable.getSimpleData();
	    	for(var i=0;i<info.length;i++){
	    		if(dw_value===info[i].co_name){
	    			co_code=info[i].co_code;
	    			parent_co_id=info[i].parent_co_id;
	    			break;
	    		}
	    	}	    	
	    	var queryData={"nd":nd_value,"co_code":co_code,"parent_co_id":parent_co_id};
	    	$.ajax({
	    		type:'POST',
	    		url:ctrlBasePath+'/data_status/allRowInfo',
	    		data:queryData,
	    		dataType:'json',
	    		success:function(result){
	    			var data=result.data;
	    			if(data!=null){
	    				for(var i=0;i<data.length/6;i++){
	    					viewModel.mainDataTable.addSimpleData([
                                {"dw_code":data[i*6+0],"dw_name":data[i*6+1],"p_dw_code":data[i*6+2],"acc_book":data[i*6+3],"gl_def":data[i*6+4],"asset_card":data[i*6+5]}
	    					]);
	    				}
	    				var my = viewModel.mainDataTable.getSimpleData();
	    				console.log(my);
	    				console.log(my[1].dw_name);
	    			}
	    			else{
	    			}
	    		},
	    		error:function(){
	    			console.log("列表信息出错");
	    		}
	    	});
	    }
	  //获取选择路径，并导出excel
	    viewModel.saveExcel=function(){
	    	var co_code="";
	    	var parent_co_id="";
	    	//var savePath="";
	    	var nd=$("#select_nd_info").val();  //选择年度	    	
	    	var dw_value = $("#select_dw_info").val();
	    	var info=viewModel.ipCompDataTable.getSimpleData();
	    	for(var i=0;i<info.length;i++){
	    		if(dw_value===info[i].co_name){
	    			co_code=info[i].co_code;
	    			parent_co_id=info[i].parent_co_id;
	    			break;
	    		}
	    	}
	    	//ajax参数定义
	    	var queryData={"nd":nd,"co_code":co_code,"parent_co_id":parent_co_id};
	    	$.ajax({
	    		type:'POST',
	    		url:ctrlBasePath+'/data_status/outPutExcel',
	    		data:queryData,
	    		dataType:'json',	    		
	    		success:function(bool){
	    			if(bool===true){
	    				alert("已成功导出excel到D盘！");
	    			}else{
	    				alert("导出excel失败！");
	    			}
	    		}
	    	});
	    }
		//下拉框的年度信息获取
		viewModel.queryNd=function(){
			var nd=$("#select_nd_info");   //html中的  nd select
			$.ajax({
				type:'GET',
				url:ctrlBasePath+'/data_status/ndInfo',
				dataType:'json',
				data:{},
				success:function(result){					
					var data=result.data; //Controller层传回来的data,data是List<String>集合
					nd.html("");
					var choice_nd='<option selected>请选择会计年度</option>';
					nd.append(choice_nd);
					for(var i=0;i<data.length;i++){
						var temp='<option>'+data[i]+'</option>';
						nd.append(temp);
					}
				}
			});
		}
		//下拉框的部门名称信息获取
		viewModel.queryDwName=function(){
			var dw=$("#select_dw_info"); //html中的dw select
			$.ajax({
				type:'GET',
				url:ctrlBasePath+'/data_status/coNameInfo',
				dataType:'json',
				data:{},
				success:function(result){
					var data=result.data;       //从Controller层获取响应数据,data是List<IpComp>类型
					viewModel.ipCompDataTable.setSimpleData(data);
					dw.html("");
					var choice_dw='<option selected>请选择单位</option>';
					dw.append(choice_dw);
					for(var i=0;i<data.length;i++){
						var temp='<option>'+data[i].co_name+'</option>';
						dw.append(temp);
					}
					
				}
			});
		}
		
	//页面初始化
	var init=function () {
		 viewModel.md = document.querySelector('#demo-mdlayout');
		 viewModel.queryNd();
		 viewModel.queryDwName();
		// viewModel.testDataTable.getSimpleData();
		 //viewModel.queryRowListInfo();
		 ko.cleanNode($('.content')[0]);   //清除绑定
         app = u.createApp(
             {
                 el: '#content',
                 model: viewModel
             }
         );
	}
	//定义返回值
	return {
        'model': viewModel,
        'template': template,
        'init': init
    };
    
});